﻿using System;
using System.Text;
using System.Web;

namespace UcAsp.Net.Comm
{
    public class Encrypt
    {
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        public static string EncodeEncrypt(string word)
        {
            byte[] byt = Encoding.UTF8.GetBytes(word + "|" + DateTime.Now.ToString());
            string result = Convert.ToBase64String(byt);

            return HttpUtility.UrlEncode(result);
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        public static string DecodeEncrypt(string word)
        {
            byte[] byt = Encoding.UTF8.GetBytes(HttpUtility.UrlDecode(word));
            return Encoding.UTF8.GetString(Convert.FromBase64String(word));
        }

        //信息写入WINDOWS日志
        public static void ErrorLog(string sourceName, string message, bool isError)
        {
            System.Diagnostics.EventLog eventLog = null;
            // 确定日志是否存在
            if (!(System.Diagnostics.EventLog.SourceExists(sourceName)))
            {
                System.Diagnostics.EventLog.CreateEventSource(sourceName, sourceName + "Log");
            }
            if (eventLog == null)
            {
                eventLog = new System.Diagnostics.EventLog(sourceName + "Log");
                eventLog.Source = sourceName;
            }

            // 记录日志信息
            if (isError)
            {
                eventLog.WriteEntry(message, System.Diagnostics.EventLogEntryType.Error);
            }
            else
            {
                eventLog.WriteEntry(message, System.Diagnostics.EventLogEntryType.Information);
            }
        }
    }
}